package com.wms.consumer.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * Description: TODO 生产者推送消息
 *
 * @version 1.0
 * @author: wangqiang
 * @date: 2023/2/19 1:57
 */
@RestController
public class SendMessageController {

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * Description: TODO 生产者推送消息到直连交换机,发送的消息中携带路由键为TestDirectRouting ,才会分发到指定的队列
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:02
     */
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "生产者发送消息成功!";
    }

    /**
     * Description: TODO 生产者推送消息到主题交换机,发送的消息中携带路由键为topic.man ,才会分发到指定的队列
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:02
     */
    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "ok";
    }

    /**
     * Description: TODO 生产者推送消息到主题交换机,发送的消息中携带路由键为topic.woman ,才会分发到指定的队列
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:02
     */
    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "ok";
    }

    /**
     * Description: TODO 生产者推送消息到扇形交换机,经交换机绑定的多个队列,都可以接收到消息
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:02
     */
    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "ok";
    }

    /**
     *  测试消息确认回调函数会在什么情况触发呢？
     */

    /**
     * Description: TODO    ①消息推送到server，但是在server里找不到交换机  (结论： ①这种情况触发的是 ConfirmCallback 回调函数。)
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:36
     */
    @GetMapping("/testMessageAck")
    public String testMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * Description: TODO     ②消息推送到server，找到交换机了，但是没找到队列  (结论： 触发的是 ConfirmCallback和RetrunCallback两个回调函数。)
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:36
     */
    @GetMapping("/testMessageAck2")
    public String testMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    /**
     * Description: TODO     ③消息推送到sever，交换机和队列啥都没找到  (结论： ③这种情况触发的是 ConfirmCallback 回调函数。)
     * @author: wangqiang
     * @return: java.lang.String
     * @date: 2023/2/19 23:36
     */
    @GetMapping("/testMessageAck3")
    public String testMessageAck3() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("no_exists_lonelyDirectExchange", "no_exists_testDirectRouting", map);
        return "ok";
    }
}
